package com.example.demo.practice;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.example.demo.practice.tool.ImageGroupID;
import com.example.demo.practice.ui.MenuTitle;

import java.util.Objects;

/**
 * Created by dm on 16-3-29.
 * 第一个页面
 */
public class Table3 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab03, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initGridView();
    }

    /**
     * 初始化网格视图
     */
    private void initGridView() {
        MenuTitle menuTitle1=(MenuTitle) getActivity().findViewById(R.id.area_divide);
        MenuTitle menuTitle2=(MenuTitle) getActivity().findViewById(R.id.area_query);
        MenuTitle menuTitle3=(MenuTitle) getActivity().findViewById(R.id.area_details);

        ImageGroupID imageGroupID=new ImageGroupID(getActivity());
        int[] icon1 = imageGroupID.getImageGroupID(R.array.area_divide_image);
        int[] icon2 = imageGroupID.getImageGroupID(R.array.area_query_image);
        int[] icon3 = imageGroupID.getImageGroupID(R.array.area_details_image);

        String[] iconName1 = getResources().getStringArray(R.array.area_divide);
        String[] iconName2=getResources().getStringArray(R.array.area_query);
        String[] iconName3=getResources().getStringArray(R.array.area_details);

        menuTitle1.setView(icon1,iconName1);
        menuTitle2.setView(icon2,iconName2);
        menuTitle3.setView(icon3,iconName3);

        menuTitle1.setOnItemClickListener(new Listener1());
        menuTitle2.setOnItemClickListener(new Listener2());
        menuTitle3.setOnItemClickListener(new Listener3());
    }

    private class Listener1 implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String activityName = "";
            switch (i) {
                case 0:
                    activityName = "com.example.demo.practice.area.AdDivision";
                    break;
                case 1:
                    activityName = "com.example.demo.practice.area.LatLngGrid";
                    break;
                case 2:
                    activityName = "com.example.demo.practice.area.OtherDivision";
                    break;
                default:
                    break;
            }
            //动态界面跳转
            if (!Objects.equals(activityName, "")) {
                try {
                    Intent intent = new Intent(getActivity(), Class.forName(activityName));
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private class Listener2 implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String activityName = "";
            switch (i) {
                case 0: activityName = "com.example.demo.practice.area.AreaStatistic";
                    break;
                case 1:activityName = "com.example.demo.practice.area.AreaLists";
                    break;
                default:
                    break;
            }
            //动态界面跳转
            if (!Objects.equals(activityName, "")) {
                try {
                    Intent intent = new Intent(getActivity(), Class.forName(activityName));
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private class Listener3 implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String activityName = "";
            switch (i) {
                case 0:
                    activityName = "com.example.demo.practice.dashboardview.ChargePerson";
                    break;
                case 1:
                    activityName = "com.example.demo.practice.area.KeyGoalActivity";
                    break;
                default:
                    break;
            }
            //动态界面跳转
            if (!Objects.equals(activityName, "")) {
                try {
                    Intent intent = new Intent(getActivity(), Class.forName(activityName));
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
